Purpose

This script creates a quick visualization of user-labeled shoreline contamination (SC) and creates a new shapefile that has been edited to remove areas where SC has been detected so that we can add some uncertainty measures later in the model.

## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
## 
## The legacy packages maptools, rgdal, and rgeos, underpinning this package
## will retire shortly. Please refer to R-spatial evolution reports on
## https://r-spatial.org/r/2023/05/15/evolution4.html for details.
## This package is now running under evolution status 0
## [[1]]
##  [1] "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
##  [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics" 
## [13] "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[2]]
##  [1] "sf"        "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"    
##  [7] "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"    
## [13] "graphics"  "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[3]]
##  [1] "mapview"   "sf"        "lubridate" "forcats"   "stringr"   "dplyr"    
##  [7] "purrr"     "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse"
## [13] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"  
## [19] "base"

Load the labels file into the environment

labels_file <- list.files('data/labels/', full.names = T)
labels <- read_csv(labels_file)
## Rows: 7862 Columns: 15
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr   (3): mission, class, vol_init
## dbl  (11): lat, lon, Red, Green, Blue, B5, B6, B7, B8, B11, B12
## date  (1): date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Load the aoi into the environment

aoi <- st_read('data/aoi/Superior_AOI_modeling.shp')
## Reading layer `Superior_AOI_modeling' from data source 
##   `/Users/steeleb/Documents/GitHub/Superior-Plume-Bloom/data/aoi/Superior_AOI_modeling.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 1 feature and 1 field
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -92.09412 ymin: 46.56115 xmax: -90.1 ymax: 47.30476
## Geodetic CRS:  NAD83

Grab the shoreline contamination labels and plot on ma Filter for ‘shorelineContamination’

sc_labels <- labels %>%
  filter(class == 'shorelineContamination')

The crs is WGS 84, so let’s save these as a sf

sc <- st_as_sf(sc_labels, coords = c('lon', 'lat'), crs = 'EPSG:4326')

#and then convert to the crs of the aoi
aoi_crs <- st_crs(aoi)

sc <- st_transform(sc, aoi_crs)

And plot to see where they are in conjunction with the shapefile

ggplot() +
  geom_sf(data = aoi) +
  geom_sf(data = sc, color = 'red') +
  theme_minimal()

As expected, this has pretty good coverage, especially since there are 875 labels.

Let’s buffer these points by 60m and snip the polygon to account for these areas of contamination.

sc_buff <- st_buffer(sc, dist = 60)
# then perform a union so that there's only one feature
sc_buff_1 <- st_union(sc_buff)
# check to make sure
mapview(aoi) +
  mapview(sc_buff_1)

And now remove those areas from the polygon

# make geometries valid
aoi <- st_make_valid(aoi)
sc_buff_1 <- st_make_valid(sc_buff_1)

# remove areas of sc_buff from original aoi
aoi_no_sc <- st_difference(aoi, sc_buff_1)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
mapview(aoi_no_sc)

And let’s remove the tiny areas that are created that are not part of the large polygon.

# get indiv polygons
aoi_no_sc_all <- st_cast(aoi_no_sc, 'POLYGON')
## Warning in st_cast.sf(aoi_no_sc, "POLYGON"): repeating attributes for all
## sub-geometries for which they may not be constant
# calculate the area for each
aoi_no_sc_all$area <- st_area(aoi_no_sc_all)
# sort them by size and only grab the largest one
aoi_no_sc_one <- aoi_no_sc_all %>%
  arrange(-area) %>%
  slice(1) %>%
  st_make_valid(.)

mapview(aoi_no_sc_one)

And now trim the SC layer to the modeling AOI.

sc_buff_export <- sc_buff_1 %>%
  st_intersection(., aoi) %>%
  st_make_valid(.)

mapview(sc_buff_export)

And finally, write the shapefiles

st_write(sc_buff_export, 'data/aoi/Superior_shoreline_contamination.shp', append = F)
## Deleting layer `Superior_shoreline_contamination' using driver `ESRI Shapefile'
## Writing layer `Superior_shoreline_contamination' to data source 
##   `data/aoi/Superior_shoreline_contamination.shp' using driver `ESRI Shapefile'
## Writing 1 features with 0 fields and geometry type Multi Polygon.
st_write(aoi_no_sc, 'data/aoi/Superior_AOI_minus_shoreline_contamination.shp', append = F)
## Deleting layer `Superior_AOI_minus_shoreline_contamination' using driver `ESRI Shapefile'
## Writing layer `Superior_AOI_minus_shoreline_contamination' to data source 
##   `data/aoi/Superior_AOI_minus_shoreline_contamination.shp' using driver `ESRI Shapefile'
## Writing 1 features with 1 fields and geometry type Multi Polygon.